<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'royalty.add'"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'royalty.delete'"></el-button>
                <el-button type="primary" @click="plExport" v-auth="'royalty.export'">批量导入</el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search" style="flex-wrap:wrap;">
                    <!-- <el-cascader v-model="search.salesman" :options="depts" :props="deptsProps" clearable  placeholder="请选择工作人员"></el-cascader> -->
                    <el-select v-model="search.salesman" filterable clearable placeholder="请选择工作人员">
                        <el-option v-for="item in workList" :key="item.id" :label="item.name" :value="item.id"/>
                    </el-select>
                    <el-select v-model="search.jobs_id" filterable clearable placeholder="请选择岗位">
                        <el-option v-for="item in jobList" :key="item.id" :label="item.name" :value="item.id"/>
                    </el-select>
                    <el-date-picker v-model="search.create_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column label="项目编号" prop="project_code" align="center"></el-table-column>
                <el-table-column label="会议名称" prop="metting" align="center"></el-table-column>
                <el-table-column label="客户名称" prop="customer_name" align="center"></el-table-column>
                <el-table-column label="酒店名称" prop="hotel_name" align="center"></el-table-column>
                <el-table-column label="宴会厅" prop="banquet_name" align="center"></el-table-column>
                <el-table-column label="明细表" type="expand" prop="expand" align="center" width="70">
                    <template #default="props">
                        <div m="4">
                            <scTable :data="props.row.staffCommission" hidePagination hideDo border>
                                <el-table-column width="150" />
                                <el-table-column label="工作人员" prop="admin.name" align="center"></el-table-column>
                                <el-table-column label="岗位" prop="jobs.name" align="center"></el-table-column>
                                <el-table-column label="利润" prop="project.profit" align="center"></el-table-column>
                                <el-table-column label="利润率" prop="project.profit_margin" align="center"></el-table-column>
                                <el-table-column label="提成比例" prop="commission_ratio" align="center"></el-table-column>
                                <el-table-column label="提成金额" prop="commission_money" align="center"></el-table-column>
                                <el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
                                <el-table-column label="操作" align="center">
                                    <template #default="scope">
                                        <el-button-group>
                                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'royalty.show'">查看</el-button>
                                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'royalty.edit'">编辑</el-button>
                                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                                <template #reference>
                                                    <el-button text type="danger" size="small" v-auth="'royalty.delete'">删除</el-button>
                                                </template>
                                            </el-popconfirm>
                                        </el-button-group>
                                    </template>
                                </el-table-column>
                            </scTable>
                        </div>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="所属会议" prop="project.project_code" align="center"></el-table-column>
                <el-table-column label="工作人员" prop="admin.name" align="center"></el-table-column>
                <el-table-column label="岗位" prop="jobs.name" align="center"></el-table-column>
                <el-table-column label="利润" prop="project.profit" align="center"></el-table-column>
                <el-table-column label="利润率" prop="project.profit_margin" align="center"></el-table-column>
                <el-table-column label="提成比例" prop="commission_ratio" align="center"></el-table-column>
                <el-table-column label="提成金额" prop="commission_money" align="center"></el-table-column>
                <el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'royalty.show'">查看</el-button>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'royalty.edit'">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'royalty.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column> -->
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <expoet-all v-if="dialog.expoet" ref="expoetDialog" @success="handleSuccess" @closed="dialog.expoet = false"></expoet-all>
</template>
<script>
import saveDialog from './save.vue'
import ExpoetAll from '@/views/expoetAll.vue'
export default {
	name: '客户管理',
	components: {
		saveDialog,ExpoetAll,
	},
	data() {
		return {
			dialog: {
				save: false,
				expoet: false,
			},
			apiObj: this.$API.project.royalty.adminList,
			selection: [],
			search: {
				salesman: null,
				jobs_id: null,
				create_time: null,
			},
            groupId:"0",
            workList:[],
            jobList:[],
		}
	},
	mounted() {
        this.getUser()
        this.getJobs()
	},
	methods: {
        success(res){
			if(res.code==1){
				this.$refs.table.refresh()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
        plExport(){
            this.dialog.expoet = true
            this.$nextTick(() => {
                this.$refs.expoetDialog.open('royalty').setData('https://2171.mzth.cn/uploads/files/20240813/20240813114610dd69f9194.xlsx')
            })
        },
		//删除
		async table_del(row) {
			var reqData = {id: row.id}
			var res = await this.$API.project.royalty.delete.post(reqData);
			if (res.code == 1) {
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.project.royalty.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'royalty') {
				this.$refs.table.refresh()
			}
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        tabChange(e){
            this.groupId = e
        },
        async getUser(){
            var res = await this.$API.system.admin.all.get();
            this.workList = res.data;
        },
        async getJobs(){
            var res = await this.$API.system.jobs.list.get();
            this.jobList = res.data;
        },
	}
}
</script>

<style>
.titleBox {
	font-size: 14px;
	font-weight: 500;
	color: #0099ff;
	margin-left: 30px;
}
</style>
